package com.vsd.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.bean.Ret;
import com.demo.dialet.PaginationSupport;
import com.demo.domain.Notify;
import com.demo.domain.UserNotify;
import com.demo.util.SqlUtils;
import com.vsd.daoEx.model.NotifyEx;
import com.vsd.domain.User;
import com.vsd.repository.NotifyRepository;
import com.vsd.repository.UserNotifyRepository;
import com.vsd.repository.UserRepository;
import com.vsd.service.NotifyService;

/**
 * 用户服务接口实现
 * 
 * @author jiekechoo
 *
 */
@Service
@Transactional
public class NotifyServiceImpl implements NotifyService {
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(NotifyServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private NotifyRepository notifyRepository;
	@Autowired
	private UserNotifyRepository userNotifyRepository;
	@Autowired
	UserRepository userRepository;

	@Override
	public Page<Notify> findByStateAndUserIdIsNull(Integer state, Pageable p) {
		// TODO Auto-generated method stub
		return notifyRepository.findByStateAndUserIdIsNull(state, p);
	}

	@Override
	public List<NotifyEx> selectExList() {
		// TODO Auto-generated method stub
		String sql = "select n.*,u.nick_name as nickName,u.username as account from b_notify n left join f_user u on n.user_id = u.id"
				+ " where 1=1 order by n.create_time desc";
		
		sql = PaginationSupport.initPage(sql, jdbcTemplate);
		
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
		List<NotifyEx>  ret = new ArrayList<>();
		for (Map<String, Object> map : maps) {
			NotifyEx ex = new NotifyEx();
			SqlUtils.transMap2Bean2(map, ex);
			ret.add(ex);
		}
		
		return ret;
	}

	@Override
	public void save(Notify n) {
		// TODO Auto-generated method stub
		notifyRepository.save(n);
	}

	@Override
	public Notify findOne(String id) {
		// TODO Auto-generated method stub
		return notifyRepository.findOne(id);
	}

	@Override
	public int updateByPrimaryKeySelective(Notify n) {
		// TODO Auto-generated method stub
		try {
			String sql = SqlUtils.update(n, true, " where id = '"+n.getId()+"'");
			return jdbcTemplate.update(sql);
		}catch (Exception e) {
			return 0;
		}
	}

	@Override
	public void delById(String id) {
		// TODO Auto-generated method stub
		notifyRepository.delete(id);
	}

	@Override
	public Ret sendNotify(String id) {
		// TODO Auto-generated method stub
		Notify notify = notifyRepository.findOne(id);
		if(notify!=null){
			UserNotify record = new UserNotify();
			record.setType(1);
			record.setContent(notify.getContent());
			record.setCreateTime(new Date());
			record.setNid(notify.getId());
			record.setIsRead(0);
			record.setTitle(notify.getTitle());
			record.setRedirectType(notify.getRedirectType());
			record.setRedirectContent(notify.getRedirectContent());
			if(StringUtils.isNotBlank(notify.getUserId())){
				User user = userRepository.findOne(notify.getUserId());
				if(user == null)return new Ret(1,"用户不存在");
				record.setUserId(notify.getUserId());
				userNotifyRepository.save(record);
				notify.setNum(1);
			}else{
//				UserExample example = new UserExample();
//				List<User> list = userMapper.selectByExample(example);
//				for (User user : list) {
//					record.setId(null);
//					record.setUserId(user.getId());
//					userNotifyMapper.insert(record);
//				}
//				notify.setNum(list.size());
			}
			
			notify.setState(2);
			updateByPrimaryKeySelective(notify);
			return new Ret(0);
		}
		return new Ret(0);
	}

	@Override
	public void deleteEx(String id) {
		// TODO Auto-generated method stub
		String sql = "delete from b_user_notify where nid = '"+id+"'";
		jdbcTemplate.execute(sql);
		notifyRepository.delete(id);
	}

	
}
